#!/usr/bin/env python
import fileinput,time,os

class  Logs:

    def __init__(self,filename):
        today=time.strftime("%Y%m%d",time.localtime())
        self.log_home_path = "./logs/"+today
        #self.log_home_path = "../logs/"+today
        #self.log_home_path='.'
        self.logfile = self.log_home_path +"/" + filename
        print "self.log_home_path : %s " %  self.log_home_path
        if (not os.path.exists(self.log_home_path)):
            os.mkdir(self.log_home_path)

    def read(self):
        with open(self.logfile, 'r') as Mes:
            return Mes.readlines()

    def fileinput(self,old,new):
        Today_time=time.strftime("%Y%m%d%H%M%S",time.localtime())
        for line in fileinput.input(self.logfile,backup='-%s'%Today_time,inplace=1):
            print line.rstrip().replace(old,new)

    def add(self,addMes):
        with open(self.logfile,'a+') as Mes:
            Mes.write(addMes)

    def write(self,addMes):
        with open(self.logfile,'w') as Mes:
            Mes.write(addMes)

